﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Layered.Domain.Asset;
using Eassets.Service.Asset;
using System.Collections.ObjectModel;
using Eassets.Service;
using Eassets.Service.Global;
using Eassets.Layered.Domain.Global;

namespace Eassets.UI.Asset
{
    public partial class OrganizationLevelListAssetForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAllMainCategory();
                // GetAllOrganizationLevelAssetofSelectedMainCategory(Convert.ToInt64(dropDownListMainCategory.SelectedValue));
                GetOrganizationLevelList();
                //GetAllAssets();
                GetAssets();
            }
        }

        private void GetOrganizationLevelList()
        {
            OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
            List<OrganizationLevelList> organizationLevelLists = new List<OrganizationLevelList>();
            organizationLevelLists = organizationLevelListService.GetAllOrganizationLevelList().Where(x => x.OrganizationLevelMode == "B").ToList();
            dropDownListOrganizationLevelList.DataSource = organizationLevelLists;
            dropDownListOrganizationLevelList.DataValueField = "OrganizationLevelListId";
            dropDownListOrganizationLevelList.DataTextField = "Code";
            dropDownListOrganizationLevelList.DataBind();
        }

        protected void dropDownListOrganizationLevelList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void GetAllOrganizationLevelAssetofSelectedMainCategory(long AssetCategoryId)
        {
            CompanyAssetService companyAssetService = new CompanyAssetService();
            List<CompanyAsset> companyAssets = new List<CompanyAsset>();
            companyAssets = companyAssetService.GetAllCompanyAssets().Where(x => (x.Asset.AssetCategoryId == AssetCategoryId)).ToList();
            gridViewOrganizationLevelAsset.DataSource = companyAssets;
            gridViewOrganizationLevelAsset.DataBind();

        }

        private void GetAllMainCategory()
        {
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListMainCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == 0).ToList();

                dropDownListMainCategory.DataValueField = "CategoryId";
                dropDownListMainCategory.DataTextField = "Description";
                dropDownListMainCategory.DataBind();

            }
            catch (Exception exception)
            {
                throw;
            }
        }

        private void GetAllAssets()
        {
            AssetService assetService = new AssetService();
            Assets asset = new Assets();
            try
            {
                gridViewOrganizationLevelAsset.DataSource = assetService.GetAllAsset();
                gridViewOrganizationLevelAsset.DataBind();

            }
            catch (Exception exception)
            {


            }
        }

        protected void dropDownListMainCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAllSubCategory();
        }

        protected void dropDownListSubCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAllAssetstoDropDown();

        }

        private void GetAllSubCategory()
        {
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListSubCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == Convert.ToInt32(dropDownListMainCategory.SelectedValue)).ToList();

                dropDownListSubCategory.DataValueField = "CategoryId";
                dropDownListSubCategory.DataTextField = "Description";
                dropDownListSubCategory.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }

        protected void dropDownListAsset_SelectedIndexChanged(object sender, EventArgs e)
        {
            OrganizationLevelListAssetService organizationLevelListAssetService = new OrganizationLevelListAssetService();
            OrganizationLevelListAssetCount organizationLevelListAssetCount = new OrganizationLevelListAssetCount();
            
            gridViewOrganizationLevelAsset.DataSource = organizationLevelListAssetService.GetAllOrganizationLevelListAssetCount().Where(x => x.AssetId == Convert.ToInt64(dropDownListAsset.SelectedValue)).ToList();
            gridViewOrganizationLevelAsset.DataBind();
        }

        private void GetAssets()
        {
            OrganizationLevelListAssetService organizationLevelAssetService = new OrganizationLevelListAssetService();
            //OrganizationLevelAssetCount organizationLevelAsset = new OrganizationLevelAsset();

            try
            {
                ViewState["OrganizationLevelListAssetCount"] = organizationLevelAssetService.GetAllOrganizationLevelListAssetCount();
                gridViewOrganizationLevelAsset.DataSource = ViewState["OrganizationLevelListAssetCount"];
                gridViewOrganizationLevelAsset.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        private void GetAllOrganizationLevelAssetBySubCategoryId()
        {
            OrganizationLevelListAssetService organizationLevelAssetService = new OrganizationLevelListAssetService();
            //gridViewOrganizationLevelAsset.DataSource = organizationLevelAssetService.GetAllOrganizationLevelAssetCount.Where(x => x.AssetCategoryId == Convert.ToInt32(dropDownListSubCategory.SelectedValue)).ToList();
            //gridViewOrganizationLevelAsset.DataBind();
        }



        private void GetAllAssetstoDropDown()
        {
            dropDownListAsset.Items.Clear();
            AssetService assetService = new AssetService();
            Assets asset = new Assets();

            try
            {
                dropDownListAsset.DataSource = assetService.GetAllAsset().Where(x => x.AssetCategoryId == Convert.ToInt64(dropDownListSubCategory.SelectedValue));
                dropDownListAsset.DataValueField = "AssetId";
                dropDownListAsset.DataTextField = "Description";
                dropDownListAsset.DataBind();
            }
            catch (Exception exception)
            {

            }
        }

        protected void ButtonItemDetails_OnClick(object sender, EventArgs e)
        {
            GridViewRow gridViewRow = (GridViewRow)((Button)sender).Parent.Parent;

            long assetId = Convert.ToInt64(gridViewRow.Cells[1].Text);
            string assetCode = gridViewRow.Cells[2].Text.Trim();
            TextBox textBoxQuantity = (TextBox)gridViewRow.Cells[4].FindControl("textBoxCount");

            long assetQuantity = Convert.ToInt16(textBoxQuantity.Text.Trim());
            Response.Redirect("FixedAssetItemDetailsForm.aspx?Parameter=" + assetId + ";" + Server.UrlEncode(dropDownListMainCategory.SelectedValue) + ";" + Server.UrlEncode(dropDownListSubCategory.SelectedValue) + ";" + assetCode + ";" + assetQuantity + ";" + "OL" + ";" + Server.UrlEncode(dropDownListOrganizationLevelList.SelectedValue));

        }

        protected void gridViewOrganizationLevelAssets_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gridViewOrganizationLevelAsset.DataSource = ViewState["OrganizationLevelListAssetCount"];
            gridViewOrganizationLevelAsset.DataBind();
            //  GetData(Convert.ToInt16(textBoxnoOfItems.Text), Convert.ToInt64(ViewState["AssetId"]), DataBindToGridView(), Convert.ToBoolean(ViewState["IsProcurement"]));
            gridViewOrganizationLevelAsset.PageIndex = e.NewPageIndex;
            gridViewOrganizationLevelAsset.DataBind();
        }
    }
}
